from flask import Flask, render_template, request, flash, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import create_engine, Column, Integer, Text, DateTime, SMALLINT, Float, Boolean
app = Flask(__name__,instance_relative_config=True,template_folder='templates')
app.config['SECRET_KEY'] = "random string"
HOSTNAME = "127.0.0.1"
PORT = 3306
USERNAME = "root"
PASSWORD = "root"
DATABASE = "flask_games"
# 拼接式创建
app.config['SQLALCHEMY_DATABASE_URI'] = f"mysql+pymysql://{USERNAME}:{PASSWORD}@{HOSTNAME}:{PORT}/{DATABASE}?charset=utf8mb4"
# 每次执行会连接到 flask库
db = SQLAlchemy(app)

class Plugin(db.Model):
    __tablename__ = "plugin"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)  # False不能为空   True可以为空
    plugin_id = db.Column(db.String(32),comment='插件id', nullable=False)
    title = db.Column(db.String(32),comment='插件名称', nullable=False)
    image = db.Column(db.Text,comment='插件图片', nullable=True)
    plugsDescribe = db.Column(db.Text,comment='插件描述', nullable=True)
    plugsType = db.Column(db.Text,comment='插件类型，多个用逗号隔开', nullable=True)
    paths = db.Column(db.Text,comment='插件路径', nullable=True)
    ctime = db.Column(db.DateTime, nullable=False, comment='创建时间')
    status = db.Column(db.String(32), nullable=True, comment='状态')

# 店铺插件
class Business(db.Model):
    __tablename__ = "business"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)  # False不能为空   True可以为空
    merchant_id = db.Column(db.String(255), comment='商家id', nullable=False)
    plugin_id = db.Column(db.Text, comment='插件id', nullable=False)  # 插件id得先存在,所以这里引用插件id
    businessId = db.Column(db.String(255), comment='店铺id', nullable=False)
    plugsConfigJson = db.Column(db.Text, comment='店铺配置', nullable=True)
    otherConfigJson = db.Column(db.Text, comment='其他配置', nullable=True)
    ctime = db.Column(db.DateTime, nullable=False, comment='创建时间')
    status = db.Column(db.String(32), nullable=True, comment='状态')

# 轮播图插件
class Banner(db.Model):
    __tablename__ = "banner"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)  # False不能为空   True可以为空
    merchant_id = db.Column(db.String(255), comment='商家id', nullable=False)
    plugin_id = db.Column(db.String(255), comment='插件id', nullable=False)  # 插件id得先存在,所以这里引用插件id
    banner_id = db.Column(db.String(32), comment='轮播图id', nullable=False)
    title = db.Column(db.String(32), comment='轮播图标题', nullable=False)
    target_data = db.Column(db.String(255), comment='跳转参数', nullable=True)
    target_url = db.Column(db.Text, comment='跳转地址', nullable=True)
    type = db.Column(db.Text, comment='轮播图类型', nullable=True)
    subhead = db.Column(db.Text, comment='轮播图副标题', nullable=True)
    image_list = db.Column(db.Text, comment='轮播图图片', nullable=True)
    ctime = db.Column(db.DateTime, nullable=False, comment='创建时间')
    status = db.Column(db.String(32), nullable=True, comment='状态')

# 公告通知
class Notice(db.Model):
    __tablename__ = "notice"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)  # False不能为空   True可以为空
    merchant_id = db.Column(db.String(255), comment='商家id', nullable=False)
    plugin_id = db.Column(db.String(255), comment='插件id', nullable=False)  # 插件id得先存在,所以这里引用插件id
    title = db.Column(db.String(255), comment='通知内容', nullable=False)
    sort = db.Column(db.Integer, comment='通知排序', nullable=True)
    startTime = db.Column(db.Integer, comment='通知时间', nullable=True)
    endTime = db.Column(db.Integer, comment='结束时间', nullable=True)
    image = db.Column(db.Text, comment='通知图片', nullable=True)
    type = db.Column(db.Text, comment='通知类型', nullable=True)
    ctime = db.Column(db.DateTime, nullable=False, comment='创建时间')
    status = db.Column(db.String(32), nullable=True, comment='状态')


# 优惠券
class Coupons(db.Model):
    __tablename__ = "coupons"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)  # False不能为空   True可以为空
    merchant_id = db.Column(db.String(255), comment='商家id', nullable=False)
    plugin_id = db.Column(db.String(255), comment='插件id', nullable=False)
    title = db.Column(db.String(255), comment='优惠券名称', nullable=False)
    type = db.Column(db.String(255), comment='优惠券类型,1普通优惠券、2口令优惠券、3积分优惠券', nullable=True)
    score = db.Column(db.String(255), comment='口令(类型为2)', nullable=True)
    moneys = db.Column(db.String(11), comment='优惠券面额', nullable=True)
    min_money = db.Column(db.String(32), comment='满多少减多少', nullable=True)
    max_number = db.Column(db.String(11), comment='每人最多领取几张', nullable=True)
    numbers = db.Column(db.Integer, comment='总共发放数量', nullable=True)
    time_lose = db.Column(db.Integer, comment='领取之后失效日期', nullable=True)
    user_number = db.Column(db.Integer, comment='已领取数', nullable=True)
    user_use = db.Column(db.Integer, comment='已使用数量', nullable=True)
    content = db.Column(db.Text, comment='领取说明', nullable=True)
    businessId = db.Column(db.Text, comment='店铺ID，0表示全部，其他表示对应店铺ID，多个店铺ID用,号隔开', nullable=True)
    ctime = db.Column(db.DateTime, nullable=False, comment='创建时间')
    status = db.Column(db.String(32), nullable=True, comment='0全部、1正常、2已过期、3已领完')


# 用户优惠券
class my_coupons(db.Model):
    __tablename__ = "my_coupons"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)  # False不能为空   True可以为空
    merchant_id = db.Column(db.String(255), comment='商家id', nullable=False)
    wechat_openid = db.Column(db.String(255), comment='用户id', nullable=False)
    coupons_id = db.Column(db.String(255), comment='优惠券id', nullable=False)
    ctime = db.Column(db.DateTime, nullable=False, comment='创建时间')
    status = db.Column(db.String(32), nullable=True, comment='状态') # 0.禁用,1.正常,2.已使用,3.已过期  使用数量用户控制

# 用户会员
class user_level(db.Model):
    __tablename__ = "user_level"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)  # False不能为空   True可以为空
    merchant_id = db.Column(db.String(255), comment='商家id', nullable=False)
    plugin_id = db.Column(db.String(255), comment='插件id', nullable=False)
    level_id = db.Column(db.String(255), comment='会员id', nullable=False)
    title = db.Column(db.String(255), comment='会员名称', nullable=False)
    type = db.Column(db.String(255), comment='会员类型,1.普通会员  2.plus会员  3.积分消费会员 4.元子消费会员 5.购买次数会员 6.经验会员 7.付费购买会员', nullable=True) #
    level_score = db.Column(db.String(255), comment='累计积分满多少分', nullable=True)
    level_order = db.Column(db.String(255), comment='累计购买达多', nullable=True)
    level_money = db.Column(db.String(255), comment='累计消费满多少元', nullable=True)
    level_experience = db.Column(db.String(255), comment='积累多少经验升级', nullable=True)
    level_payment = db.Column(db.String(255), comment='(7)付费购买金额', nullable=True)
    level_payday = db.Column(db.String(255), comment='(7)付费购买有效时间，单位天', nullable=True)
    level_logo = db.Column(db.Text, comment='会员等级logo', nullable=True)
    discount = db.Column(db.String(10), comment='折扣率，按百分比计算', nullable=True)
    ctime = db.Column(db.DateTime, nullable=False, comment='创建时间')
    status = db.Column(db.String(32), nullable=True, comment='状态0禁用 1正常 2过期(类型为7)')

# 主题
class theme(db.Model):
    __tablename__ = "theme"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)  # False不能为空   True可以为空
    merchant_id = db.Column(db.String(255), comment='商家id', nullable=False)
    plugin_id = db.Column(db.String(255), comment='插件id', nullable=False)
    main_color = db.Column(db.Text, comment='主体颜色', nullable=True)
    assist_color = db.Column(db.Text, comment='辅助颜色', nullable=True)
    type = db.Column(db.Text, comment='会员类型,1.小程序  2.app  3.pc ', nullable=True) #
    main_font_color = db.Column(db.Text, comment='主字体颜色', nullable=True)
    font_assist = db.Column(db.Text, comment='辅字体颜色', nullable=True)
    tabbar = db.Column(db.Text, comment='tabbar', nullable=True)
    ctime = db.Column(db.DateTime, nullable=True, comment='创建时间')
    status = db.Column(db.String(32), nullable=True, comment='状态0禁用 1正常 2过期(类型为7)')


# 主体活动
class main_active(db.Model):
    __tablename__ = "main_active"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)  # False不能为空   True可以为空
    merchant_id = db.Column(db.String(255), comment='商家id', nullable=False)
    plugin_id = db.Column(db.String(255), comment='插件id', nullable=False)
    content = db.Column(db.Text, comment='内容', nullable=True)
    title = db.Column(db.String(255), comment='标题', nullable=True)
    image = db.Column(db.Text, comment='图片', nullable=True)
    details = db.Column(db.Text, comment='详情', nullable=True) #
    type = db.Column(db.Text, comment='类型', nullable=True)  #
    preview = db.Column(db.Integer, comment='预览人数', nullable=True)  #
    apply = db.Column(db.Integer, comment='报名人数', nullable=True)  #
    profit = db.Column(db.Integer, comment='获取利益', nullable=True)  #
    stimulateAD = db.Column(db.Integer, comment='激励广告次数', nullable=True)  #
    transpond = db.Column(db.Integer, comment='转发数量', nullable=True)  #
    Text = db.Column(db.Text, comment='富文本', nullable=True)  #
    label = db.Column(db.Text, comment='标签', nullable=True)  #
    recommend = db.Column(db.Text, comment='推荐位(1推荐,0不推荐)', nullable=True)  #
    ctime = db.Column(db.DateTime, nullable=True, comment='创建时间')
    status = db.Column(db.String(32), nullable=True, comment='状态0禁用 1正常 2过期(类型为7)')


with app.app_context():
    db.create_all()
